package com.example.admin.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;


import com.example.admin.R;
import com.gyf.immersionbar.ImmersionBar;

import java.util.concurrent.ConcurrentLinkedQueue;

public abstract class BaseActivity extends FragmentActivity {
    /**
     * Context对象
     */
    protected Context mContext;
    /**
     * 所有已存在的Activity
     */
    protected static ConcurrentLinkedQueue<Activity> allActivity = new ConcurrentLinkedQueue<Activity>();
    protected static final int validActivityCount = 15;
    protected static ConcurrentLinkedQueue<Activity> finshActivity = new ConcurrentLinkedQueue<Activity>();
    /**
     * 标示
     */
    protected static final String TAG = "数据打印";

    boolean immersionModel = true;//是否是沉浸式状态栏，true时表示是沉浸式
    public static boolean darkStatusBar = true;//当是沉浸式状态栏时，状态栏字体是否黑色字体，false时表示白色字体，true表示黑色字体

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//舍弃标题栏
        mContext = this;
        // getonline();

        //Activity队列管理，如果超出指定个数，获取并移除此队列的头（队列中时间最长的）。
        if (allActivity.size() >= validActivityCount) {
            Activity act = allActivity.poll();
            act.finish();// 结束
        }
        allActivity.add(this);
        printAllActivityName();
        try {//设置布局文件、初始化布局文件中的控件、初始化控件的监听、进行数据初始化、处理其他的数据。（子类重写这些方法）
            setContentView(savedInstanceState);
            setInitView();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
        //setStatusBar();
        if (!immersionModel) {
            ImmersionBar.with(this)
                    .statusBarColor(R.color.white)
                    .statusBarDarkFont(true)
                    .fitsSystemWindows(true)
                    .init();
        } else {
            if (darkStatusBar) {
                ImmersionBar.with(this)
                        .statusBarDarkFont(true)
                        .init();
            } else {
                ImmersionBar.with(this).init();
            }
        }
    }

    protected void setFinshActivity(Activity context) {
        finshActivity.add(context);
    }

    protected void toast(String str) {
        Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
    }

    /**
     * 设置布局文件
     */
    public abstract void setContentView(Bundle savedInstanceState);

    public abstract void setInitView();


    private static void printAllActivityName() {
        for (Activity activity : allActivity) {
            Log.e(TAG, activity.getClass().getName());
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * 退出当前activity
     *
     * @see Activity#finish()
     */

    @Override
    public void finish() {
        try {
            super.finish();
            //软键盘隐藏
            if (null != this.getCurrentFocus() && null != this.getCurrentFocus().getWindowToken()) {
                InputMethodManager in = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
                in.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
            // 从Activity集合中清理出已结束的Activity
            if (allActivity != null && allActivity.size() > 0 && allActivity.contains(this)) {
                allActivity.remove(this);
            }
            for (Activity a : allActivity) {
                Log.e("finish", a.getClass().getName());
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    public void back(View v) {
        finish();
    }

    /**
     * 结束所有activity
     */
    public static void finishAll() {
        // 结束Activity
        try {
            for (Activity act : allActivity) {
                allActivity.remove(act);
                act.finish();
                printAllActivityName();
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    /**
     * 部分activity
     */
    public static void finishActivity() {
        // 结束Activity
        try {
            for (Activity act : finshActivity) {
                finshActivity.remove(act);
                act.finish();
                printAllActivityName();
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }
/*   @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (AppUtils.isFastDoubleClick()) {
                return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }*/

}
